/**
 * Project Name:emcs
 * File Name:ConfigurationManagementController.java
 * Package Name:com.zeei.dsas.controller
 * Date:2016年9月28日下午3:40:48
 * Copyright (c) 2016, ZEEI All Rights Reserved. *
 *
 * ClassName:ConfigurationManagementController 
 * Function: TODO ADD FUNCTION.  
 * @author   chq
 * @since    JDK 1.8	 
 */

package com.zeei.bfs.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zeei.bfs.dao.SysLogDao;
import com.zeei.bfs.service.ConfigurationManagementService;
import com.zeei.bfs.vo.GeneralCategory;
import com.zeei.bfs.vo.RoleButtonVO;
import com.zeei.bfs.vo.SystemLog;
import com.zeei.bfs.vo.UserButtonVO;
import com.zeei.common.annotation.SysLog;
import com.zeei.common.controller.BasicController;
import com.zeei.common.enums.LogType;
import com.zeei.common.utils.StringUtil;
import com.zeei.common.utils.UserOperationLog;
import com.zeei.common.vo.PointSysTypeVO;
import com.zeei.common.vo.ResultBasicVO;
import com.zeei.common.vo.ResultRowsVO;
import com.zeei.common.vo.TreeNodeVO;

@Controller
@RequestMapping(value = "/bfs/configuration")
public class ConfigurationManagementController extends BasicController {
	@Resource
	private ConfigurationManagementService configurationManagementService;

	@Resource
	private SysLogDao syslogDao;

	@ResponseBody
	@RequestMapping(value = "/dictionary", method = RequestMethod.POST)
	@SysLog(text = "查询字典信息", type = LogType.View)
	public ResultRowsVO<GeneralCategory> getDictionaryMessage(GeneralCategory generalCategoryParam) {
		PageInfo<GeneralCategory> info = configurationManagementService.dictionaryManagement(generalCategoryParam);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询字典信息 操作.", LogType.View));

		return this.createResultRowsVO(info.getList(), true, "", (int) info.getTotal());
	}

	@ResponseBody
	@RequestMapping(value = "/dictionaryedit", method = RequestMethod.POST)
	@SysLog(text = "修改字典信息", type = LogType.Edit)
	public ResultBasicVO dictionaryEdit(GeneralCategory generalCategoryParam, String oldGeneralCode) {
		boolean flag = configurationManagementService.editDictionary(generalCategoryParam, oldGeneralCode);
		String message = "修改成功";
		if (!flag) {
			message = "修改失败";
		}
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 修改字典" + generalCategoryParam.getCategoryName() + "信息 操作.", LogType.Edit));

		return this.createResultBasicVO(flag, message);
	}

	@ResponseBody
	@RequestMapping(value = "/dictionaryadd", method = RequestMethod.POST)
	@SysLog(text = "新增字典信息", type = LogType.Edit)
	public ResultBasicVO dictionaryAdd(GeneralCategory generalCategoryParam) {
		boolean flag = configurationManagementService.addDictionary(generalCategoryParam);
		String message = "新增成功";
		if (!flag) {
			message = "新增失败";
		}
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 新增字典" + generalCategoryParam.getCategoryName() + " 操作.", LogType.Add));
		return this.createResultBasicVO(flag, message);
	}

	@ResponseBody
	@RequestMapping(value = "/dictionarytype", method = RequestMethod.POST)
	@SysLog(text = "新增字典类型信息", type = LogType.Edit)
	public ResultBasicVO dictionaryTypeAdd(GeneralCategory generalCategoryParam) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 新增字典类型" + generalCategoryParam.getCategoryClass() + " 操作.", LogType.Add));
		boolean flag = configurationManagementService.addDictionaryType(generalCategoryParam);

		return this.createResultBasicVO(flag, null);
	}

	@RequestMapping(value = "/dictionarydrop", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "删除字典信息", type = LogType.Edit)
	public ResultBasicVO dictionaryDrop(String[] generalCodes) {
		List<String> list = Arrays.asList(generalCodes);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 删除字典编码为" + StringUtils.join(generalCodes, ",") + "的信息 操作.", LogType.Delete));

		return this.createResultBasicVO(configurationManagementService.dropDictionary(list), null);
	}

	@RequestMapping(value = "/validgeneral", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO validGeneralCode(String generalCode) {
		return this.createResultBasicVO(configurationManagementService.validGeneralCode(generalCode), null);
	}

	@RequestMapping(value = "/validcategory", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO validCategoryClass(String categoryClass) {
		return this.createResultBasicVO(configurationManagementService.validCategoryClass(categoryClass), null);
	}

	@RequestMapping(value = "/validcategoryname", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO validCategoryName(String categoryName) {
		return this.createResultBasicVO(configurationManagementService.validCategoryName(categoryName), null);
	}

	@ResponseBody
	@RequestMapping(value = "/systemlog", method = RequestMethod.POST)
	@SysLog(text = "查询系统日志信息", type = LogType.View)
	public ResultRowsVO<SystemLog> getSystemLogMessage(SystemLog systemLogparam) {
		PageInfo<SystemLog> info = configurationManagementService.systemLogManagement(systemLogparam);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询系统日志信息 操作.", LogType.View));

		return this.createResultRowsVO(info.getList(), true, "", (int) info.getTotal());
	}

	@ResponseBody
	@RequestMapping(value = "/categoryclass", method = RequestMethod.POST)
	@SysLog(text = "查询字典类型", type = LogType.View)
	public List<GeneralCategory> getCategoryClass(int type) {
		List<GeneralCategory> resultList = new ArrayList<GeneralCategory>();
		if (type == 0) {
			GeneralCategory info = new GeneralCategory();
			info.setCategoryClass("");
			info.setCategoryName("全部");
			resultList.add(info);
		}
		List<GeneralCategory> list = configurationManagementService.getCategoryClass();
		resultList.addAll(list);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询字典类型 操作.", LogType.View));

		return resultList;
	}

	@ResponseBody
	@RequestMapping(value = "/categoryclassbysystemtype", method = RequestMethod.POST)
	public List<GeneralCategory> queryCategoryClassBySystemType(String systemType) {
		List<GeneralCategory> resultList = new ArrayList<GeneralCategory>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("systemType", systemType);
		List<GeneralCategory> list = configurationManagementService.queryCategoryClassBySystemType(map);
		resultList.addAll(list);
		return resultList;
	}

	@ResponseBody
	@RequestMapping(value = "/categorylist", method = RequestMethod.POST)
	@SysLog(text = "查询字典类型", type = LogType.View)
	public List<GeneralCategory> getCategoryList(String categoryclass, int type) {
		List<GeneralCategory> resultList = new ArrayList<GeneralCategory>();
		if (type == 0) {
			GeneralCategory info = new GeneralCategory();
			info.setCategoryClass("");
			info.setCategoryName("全部");
			resultList.add(info);
		}
		List<GeneralCategory> list = configurationManagementService.getCategoryList(categoryclass);
		resultList.addAll(list);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询字典类型 操作.", LogType.View));

		return resultList;
	}

	@ResponseBody
	@RequestMapping(value = "/categorytreelist", method = RequestMethod.POST)
	@SysLog(text = "查询字典类型", type = LogType.View)
	public ResultRowsVO<TreeNodeVO<GeneralCategory>> getCategoryTreeList(String categoryclass, int type) {
		List<GeneralCategory> resultList = new ArrayList<GeneralCategory>();
		if (type == 0) {
			GeneralCategory info = new GeneralCategory();
			info.setCategoryClass("");
			info.setCategoryName("全部");
			resultList.add(info);
		}
		List<GeneralCategory> list = configurationManagementService.getCategoryList(categoryclass);
		List<TreeNodeVO<GeneralCategory>> nodeDataList = new ArrayList<>();
		initGeneralCategoryTree(nodeDataList, list);
		resultList.addAll(list);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询字典类型 操作.", LogType.View));

		return this.createResultRowsVO(nodeDataList, true, null, nodeDataList.size());
	}

	private void initGeneralCategoryTree(List<TreeNodeVO<GeneralCategory>> nodeDataList, List<GeneralCategory> list) {
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				GeneralCategory info = list.get(i);
				TreeNodeVO<GeneralCategory> nodeDataItem = new TreeNodeVO<GeneralCategory>();
				nodeDataItem.setId(info.getGeneralCode());
				nodeDataItem.setText(info.getCategoryName());
				nodeDataList.add(nodeDataItem);
			}
		}
	}

	@ResponseBody
	@RequestMapping(value = "/syscategorylist", method = RequestMethod.POST)
	public List<GeneralCategory> getCategoryListBySystemType(String categoryclass, String systemType) {
		if (StringUtil.isEmpty(systemType)) {
			return new ArrayList<GeneralCategory>();
		}
		List<GeneralCategory> list = configurationManagementService.getCategoryListBySystemType(categoryclass, systemType);
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/rolebuttonlist", method = RequestMethod.POST)
	public List<RoleButtonVO> getRoleButton() {
		return configurationManagementService.getRoleButton();
	}

	@ResponseBody
	@RequestMapping(value = "/userbuttonlist", method = RequestMethod.POST)
	public List<UserButtonVO> getUserButton() {
		return configurationManagementService.getUserButton();
	}

	@ResponseBody
	@RequestMapping(value = "/gettable", method = RequestMethod.POST)
	@SysLog(text = "查询数据库存在的表", type = LogType.View)
	public String getExistsTable(String tableNames) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询数据库存在的表 操作.", LogType.View));
		List<String> list = Arrays.asList(tableNames.split(","));
		List<String> info = configurationManagementService.getExistsTable(list);
		if (info == null || info.size() == 0) {
			return null;
		} else {
			return StringUtils.join(info.toArray(), ',');
		}
	}

	@ResponseBody
	@RequestMapping(value = "/getSystemTable", method = RequestMethod.POST)
	@SysLog(text = "查询数据库存在的表", type = LogType.View)
	public List<PointSysTypeVO> getSystemTable(int systemType) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 查询数据库存在的表 操作.", LogType.View));
		List<PointSysTypeVO> tableNames = configurationManagementService.getSystemTable(systemType);
		return tableNames;
	}
}
